package com.cqupt.controller;


import com.cqupt.model.dto.UserLoginDto;
import com.cqupt.model.dto.UserRegisterDto;
import com.cqupt.model.pojo.SysUser;
import com.cqupt.model.vo.Result;
import com.cqupt.model.vo.ResultCodeEnum;
import com.cqupt.service.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotBlank;
import org.hibernate.validator.constraints.Length;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@Tag(name = "用户模块")
@RequestMapping("/api/user")
@Validated
public class SysUserController {

    @Resource
    private SysUserService sysUserService;

    @PostMapping("/login")
    @Operation(summary = "用户登录接口(账号密码登录)")
    public Result<String> login(@RequestBody UserLoginDto userLoginDto) {
        String token = sysUserService.login(userLoginDto);
        return Result.build(token, ResultCodeEnum.SUCCESS);
    }

    @PostMapping("/login2")
    @Operation(summary = "用户登录接口(手机验证码登录)")
    public Result<String> login2(@RequestBody UserLoginDto userLoginDto) {
        String token = sysUserService.login2(userLoginDto);
        return Result.build(token, ResultCodeEnum.SUCCESS);
    }

    @PostMapping("/login3")
    @Operation(summary = "手机号一键登录")
    public Result<String> login3(@RequestParam String phone) {
        String token = sysUserService.login3(phone);
        return Result.build(token, ResultCodeEnum.SUCCESS);
    }


    @PostMapping("/register")
    @Operation(summary = "用户注册接口")
    public Result register(@RequestBody UserRegisterDto userRegisterDto) {
        sysUserService.register(userRegisterDto);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @PostMapping("/updateAvatar")
    @Operation(summary = "修改头像接口")
    public Result updateAvatar(@RequestParam("file") MultipartFile file) {
        String url = sysUserService.updateAvatar(file);
        return Result.build(url, ResultCodeEnum.SUCCESS);
    }

    @GetMapping("/updateNickname")
    @Operation(summary = "修改昵称接口")
    public Result updateNickname(
            @NotBlank(message = "昵称不能为空")
            @Length(max = 8, min = 1, message = "昵称长度必须在1到8个字符之间")
            @RequestParam("nickname") String nickname) {
        sysUserService.updateNickname(nickname);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @GetMapping("/updateSex")
    @Operation(summary = "修改性别接口")
    public Result updateSex(@RequestParam int sex) {
        sysUserService.updateSex(sex);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @GetMapping("/updateBirth")
    @Operation(summary = "修改出生年月接口")
    public Result updateBirth(@RequestParam String birth) {
        sysUserService.updateBirth(birth);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @GetMapping("/userInfo")
    @Operation(summary = "获取用户信息")
    public Result userInfo() {
        SysUser sysUser = sysUserService.userInfo();
        return Result.build(sysUser, ResultCodeEnum.SUCCESS);
    }

    @PostMapping("/logout")
    @Operation(summary = "用户退出")
    public Result logout(@RequestHeader(name = "token") String token) {
        sysUserService.logout(token);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

}
